<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
  xmlns:podcast="https://podcastindex.org/namespace/1.0"
  xmlns:psc="http://podlove.org/simple-chapters"
>
  <channel>
    <title><%= @podcast |> podcast_name_with_metadata() |> escaped() %></title>
    <copyright>All rights reserved</copyright>
    <link><%= podcast_url(@podcast) %></link>
    <atom:link href="<%= PodcastView.feed_url(@podcast) %>" rel="self" type="application/rss+xml" />
    <atom:link href="<%= podcast_url(@podcast) %>" rel="alternate" type="text/html" />
    <language>en-us</language>
    <description><%= @podcast |> podcast_full_description() |> escaped() %></description>
    <itunes:author>Changelog Media</itunes:author>
    <itunes:summary><%= @podcast |> podcast_full_description() |> escaped() %></itunes:summary>
    <itunes:explicit>no</itunes:explicit>
    <itunes:image href="<%= PodcastView.cover_url(@podcast) %>"/>
    <itunes:owner>
      <itunes:name>Changelog Media</itunes:name>
      <itunes:email>editors@changelog.com</itunes:email>
    </itunes:owner>
    <itunes:keywords><%= @podcast.keywords %></itunes:keywords>
    <itunes:category text="Technology">
      <itunes:category text="Software How-To"/>
      <itunes:category text="Tech News"/>
    </itunes:category>
  <%= if @podcast.slug == "brainscience" do %>
    <itunes:category text="Health &amp; Fitness">
      <itunes:category text="Mental Health"/>
    </itunes:category>
  <% end %>
  <%= if @podcast.slug == "founderstalk" do %>
    <itunes:category text="Business">
      <itunes:category text="Careers"/>
      <itunes:category text="Entrepreneurship"/>
    </itunes:category>
  <% end %>
  <%= if @podcast.slug == "news" do %>
    <itunes:category text="News">
      <itunes:category text="Tech News"/>
    </itunes:category>
  <% end %>
    <podcast:funding url="https://changelog.com/++">Support our work by joining Changelog++</podcast:funding>
    <%= render_many(@podcast.active_hosts, __MODULE__, "_person.xml", as: :person, role: "host") %>
  <%= for episode <- @episodes do %>
    <item>
      <title><%= episode_title(@podcast, episode) |> escaped() %></title>
      <link><%= url(~p"/#{episode.podcast.slug}/#{episode.slug}") %></link>
      <guid isPermaLink="false"><%= EpisodeView.guid(episode) %></guid>
      <pubDate><%= TimeView.rss(episode.published_at) %></pubDate>
      <enclosure url="<%= EpisodeView.audio_url(episode) %>" length="<%= episode.audio_bytes %>" type="audio/mpeg" />
      <description><%= SharedHelpers.md_to_text(episode.summary) %></description>
      <content:encoded><%= render("_show_notes.xml", episode: episode, plusplus: false) %></content:encoded>

      <itunes:episodeType><%= episode.type %></itunes:episodeType>
      <itunes:image href="<%= EpisodeView.cover_url(episode) %>"/>
      <itunes:duration><%= TimeView.duration(episode.audio_duration) %></itunes:duration>
      <itunes:explicit>no</itunes:explicit>
      <itunes:keywords><%= episode.podcast.keywords %></itunes:keywords>
      <itunes:subtitle><%= escaped(episode.subtitle) %></itunes:subtitle>
      <itunes:summary><%= SharedHelpers.md_to_text(episode.summary) %></itunes:summary>
      <dc:creator>Changelog Media</dc:creator>
      <itunes:author>Changelog Media</itunes:author>
      <%= render_many(episode.hosts, __MODULE__, "_person.xml", as: :person, role: "host") %>
      <%= render_many(episode.guests, __MODULE__, "_person.xml", as: :person, role: "guest") %>
    <%= if Episode.has_transcript(episode) do %>
      <podcast:transcript url="<%= url(~p"/#{episode.podcast.slug}/#{episode.slug}/transcript") %>" type="text/html" />
    <% end %>
    <%= if Enum.any?(episode.audio_chapters) do %>
      <podcast:chapters url="<%= url(~p"/#{episode.podcast.slug}/#{episode.slug}/chapters") %>" type="application/json+chapters" />
      <psc:chapters version="1.1" xmlns="http://podlove.org/simple-chapters">
      <%= for chapter <- episode.audio_chapters do %>
        <psc:chapter start="<%= round(chapter.starts_at) %>" title="<%= chapter.title |> html_escape() |> safe_to_string() %>" href="<%= chapter.link_url |> html_escape() |> safe_to_string() %>" image="<%= chapter.image_url |> html_escape() |> safe_to_string() %>" />
      <% end %>
      </psc:chapters>
    <% end %>
    <%= if episode.socialize_url do %>
      <podcast:socialInteract uri="<%= episode.socialize_url %>" protocol="activitypub" />
    <% end %>
    </item>
  <% end %>
  </channel>
</rss>
